
#ifndef __heightmapH__
#define __heightmapH__

#include "image.h"
#include "pmodel.h"

namespace WtfEngine {
	namespace PhysicalModels {
		class StaticHeightmapModel: public PhysicalModel {
		public:
			StaticHeightmapModel(const WtfEngine::StringPool::Entry& sHmName, bool bRegisterReconstructor = true);

			static WtfEngine::IDataObject * CreateFromHm(const WtfEngine::StringPool::Entry& sName, const WtfEngine::StringPool::Entry& sHmName);

			DATA_RECONSTRUCTIBLE(StaticHeightmapModel,
				GC_ADDEDSIZE_AUTO(PhysicalModel, StaticHeightmapModel));
		};
	};

	class Heightmap: public DataObject {
		tScalar * mvfHeight;
		iVector2D mvDim;

	public:
		Heightmap(const WtfEngine::StringPool::Entry& sName): DataObject(sName), mvfHeight(NULL) {};
		~Heightmap();

		/// Loads the heightmap from a heightmap image
		void ReadImage(const Image::tRef& rImg);
		void Destroy();

		inline tScalar getHeight(unsigned i, unsigned j) const {
			assert(i < mvDim[0] && j < mvDim[1]);
			return mvfHeight[i + (mvDim[0] * j)];
		};
		inline tScalar getHeight(tScalar x, tScalar z, const tVector& vScale) const {
			return getHeight(x / vScale[0], z / vScale[2]) * vScale[1];
		};

		PhysicalModels::StaticHeightmapModel::tCacheRef getStaticModel() const;

		tVector CalculateScale(const tVector& vDesiredDim) const;

		static WtfEngine::IDataObject * Load(const StringPool::Entry& sName);

		DATA_LOADABLE(Heightmap, GC_ADDEDSIZE(DataObject,
			sizeof(Heightmap) + (sizeof(tScalar) * mvDim[0] * mvDim[1])));
	};
};

#endif
